package cn.cocho.custom.yunCashierDf.merchant.controller;

import cn.cocho.common.web.app.JsonResultContants;
import cn.cocho.common.web.jqgrid.JQGridContants;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridRules;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.cache.CacheCtrl;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantCount;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantTransactionInfo;
import cn.cocho.custom.yunCashierDf.merchant.service.MerchantTransactionInfoService;
import cn.cocho.custom.yunCashierDf.uc.domain.UcUserInfo;
import cn.cocho.custom.yunCashierDf.uc.service.UserInfoService;
import cn.cocho.custom.yunCashierDf.utils.JsonResult;
import cn.cocho.custom.yunCashierDf.utils.execl.XssfExcelHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xiaomage on 2017/6/2.
 */
@Controller
public class MerchantTransactionInfoController {
    @Autowired
    private MerchantTransactionInfoService merchantTransactionInfoService;

    @Autowired
    CacheCtrl cacheCtrl;

    @Autowired
    UserInfoService userService;

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/getMerchantTransactionInfo")
    public JQGridPage getMerchantTransactionInfo(JQGridPage<MerchantTransactionInfo> pageJQGrid,String startTime,String endTime,String terminalid,String paymentTypeName,String chcdName,String origOrderNum,String respcd)throws ParseException {
        System.out.println("我的商户ID是："+cacheCtrl.getTokenCtrl().getCurrentUcUserInfo().getUserId());
        UcUserInfo ucUserInfo = userService.getUserInfoById(cacheCtrl.getTokenCtrl().getCurrentUcUserInfo().getUserId());
        if(!ucUserInfo.getRoles().equals("ROOT")){
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("merchant_id", "eq", ucUserInfo.getMerchantId()));
        }
       // System.out.println(startTime+"||"+endTime);
        if(startTime!=null&&endTime!=null&&!startTime.equals("")&&!endTime.equals("")){
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date sTime=null;
            Date eTime=null;
            sTime=dateFormat1.parse(startTime);
            eTime=dateFormat1.parse(endTime);
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("create_time", "in", sTime.getTime()+""));
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("create_time", "ni", eTime.getTime()+""));
        }
        if(terminalid!=null&&!terminalid.equals("")){
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("terminalid", "eq", terminalid));
        }
        if(paymentTypeName!=null&&!paymentTypeName.equals("")){
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("payment_type", "eq", paymentTypeName));
        }
        if(chcdName!=null&&!chcdName.equals("")){
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("chcd", "eq", chcdName));
        }
        if(origOrderNum!=null&&!origOrderNum.equals("")){
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("orig_order_num", "eq", origOrderNum));
        }
        if(respcd!=null&&!respcd.equals("")){
            pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("respcd", "eq", respcd));
        }

        return merchantTransactionInfoService.getListByJQgrid(pageJQGrid);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/editMerchantTransactionInfo")
    public void editMerchantInfo(MerchantTransactionInfo merchantTransactionInfo, HttpServletResponse response) {
        String warnMessage = null;
        if (merchantTransactionInfo.getOper().equals(JQGridContants.EDIT_OPER_ADD)) {
            merchantTransactionInfoService.add(merchantTransactionInfo);
        } else if (merchantTransactionInfo.getOper().equals(JQGridContants.EDIT_OPER_UPDATE)) {
            merchantTransactionInfo.setMerchantId(merchantTransactionInfo.getId());
            merchantTransactionInfoService.update(merchantTransactionInfo);
        } else if (merchantTransactionInfo.getOper().equals(JQGridContants.EDIT_OPER_DEL)) {
            JQGridUtils.delete("delete from merchant_transaction_info where transaction_id ", merchantTransactionInfo.getId());
        }
        JQGridUtils.warnUserMessage(warnMessage, response);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/getMerchantTransactionInfod", method = RequestMethod.POST)
    public JsonResult<MerchantTransactionInfo> getMerchantTransactionInfoById(String transactionId) {
        JsonResult<MerchantTransactionInfo> result;
        MerchantTransactionInfo merchantTransactionInfo = merchantTransactionInfoService.getMerchantTransactionInfoById(transactionId);
        if (merchantTransactionInfo != null) {
            result = new JsonResult<MerchantTransactionInfo>(JsonResultContants.SUCCESS, "获取订单信息成功！", merchantTransactionInfo);
        } else {
            result = new JsonResult<MerchantTransactionInfo>(JsonResultContants.FAIL, "无法获取订单信息！");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/getMerchantTransactionInfoAll")
    public JsonResult<List<MerchantCount>> getMerchantTransactionInfoAll(String startTime,String endTime,String terminalid,String paymentTypeName,String chcdName,String origOrderNum,String respcd) throws ParseException {
        JsonResult<List<MerchantCount>> result=null;
        System.out.println("我的商户ID是："+cacheCtrl.getTokenCtrl().getCurrentUcUserInfo().getUserId());
        UcUserInfo ucUserInfo = userService.getUserInfoById(cacheCtrl.getTokenCtrl().getCurrentUcUserInfo().getUserId());
        String  srt=" ";
        List<String> sttList=new ArrayList<String>();
        srt+="  a.merchant_id=?  ";
        sttList.add(ucUserInfo.getMerchantId());
        if(startTime!=null&&endTime!=null&&!startTime.equals("")&&!endTime.equals("")){
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date sTime=null;
            Date eTime=null;
            sTime=dateFormat1.parse(startTime);
            eTime=dateFormat1.parse(endTime);
            srt+=" and  a.create_time>?  ";
            sttList.add(sTime.getTime()+"");
            srt+=" and  a.create_time<?  ";
            sttList.add(eTime.getTime()+"");
        }
        if(terminalid!=null&&!terminalid.equals("")){
            srt+=" and  a.terminalid=?  ";
            sttList.add(terminalid);
        }
        if(paymentTypeName!=null&&!paymentTypeName.equals("")){
            srt+=" and  a.payment_type=?  ";
            sttList.add(paymentTypeName);
        }
        if(chcdName!=null&&!chcdName.equals("")){
            srt+=" and  a.chcd=?  ";
            sttList.add(chcdName);
        }
        if(origOrderNum!=null&&!origOrderNum.equals("")){
            srt+="  and a.orig_order_num=?  ";
            sttList.add(origOrderNum);
        }
        if(respcd!=null&&!respcd.equals("")){
            srt+=" and  a.respcd=?  ";
            sttList.add(respcd);
        }
        //String[]  obj={};
        String obj[]=new String[sttList.size()];
        for(int i=0,j=sttList.size();i<j;i++){
            obj[i]=sttList.get(i);
        }
        System.out.println(srt);
        List<MerchantCount> merchantList=merchantTransactionInfoService.getMerchantCount(srt,obj);
        result = new JsonResult<List<MerchantCount>>(JsonResultContants.SUCCESS, "获取成功",merchantList);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/getExcelMerchantTransactionInfo")
    public void getExcelMerchantTransactionInfo(HttpServletResponse response,String startTime,String endTime,String terminalid,String paymentTypeName,String chcdName,String origOrderNum,String respcd) throws ParseException {
        System.out.println("我的商户ID是："+cacheCtrl.getTokenCtrl().getCurrentUcUserInfo().getUserId());
        UcUserInfo ucUserInfo = userService.getUserInfoById(cacheCtrl.getTokenCtrl().getCurrentUcUserInfo().getUserId());
        String  srt=" ";
        List<String> sttList=new ArrayList<String>();
        srt+="  merchant_id=?  ";
        sttList.add(ucUserInfo.getMerchantId());
        if(startTime!=null&&endTime!=null&&!startTime.equals("")&&!endTime.equals("")){
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date sTime=null;
            Date eTime=null;
            sTime=dateFormat1.parse(startTime);
            eTime=dateFormat1.parse(endTime);
            srt+=" and  create_time>?  ";
            sttList.add(sTime.getTime()+"");
            srt+=" and  create_time<?  ";
            sttList.add(eTime.getTime()+"");
        }
        if(terminalid!=null&&!terminalid.equals("")){
            srt+=" and  terminalid=?  ";
            sttList.add(terminalid);
        }
        if(paymentTypeName!=null&&!paymentTypeName.equals("")){
            srt+=" and  payment_type=?  ";
            sttList.add(paymentTypeName);
        }
        if(chcdName!=null&&!chcdName.equals("")){
            srt+=" and  chcd=?  ";
            sttList.add(chcdName);
        }
        if(origOrderNum!=null&&!origOrderNum.equals("")){
            srt+="  and orig_order_num=?  ";
            sttList.add(origOrderNum);
        }
        if(respcd!=null&&!respcd.equals("")){
            srt+=" and  respcd=?  ";
            sttList.add(respcd);
        }
        //String[]  obj={};
        String obj[]=new String[sttList.size()];
        for(int i=0,j=sttList.size();i<j;i++){
            obj[i]=sttList.get(i);
        }
        System.out.println(srt);

        List<MerchantTransactionInfo> list = merchantTransactionInfoService.getMerchantTransactionInfoExcel(srt, obj);
        try {
            String[] fieldNames = {"transactionId","merchantName","paymentTypeName", "origOrderNum","chcdName","castTxamt","terminalid","respcdName","createTime"};
            String[] titles ={"ID","商户名称", "交易类型","订单号","渠道","订单金额","收款人","交易结果","记录创建时间"};
            XssfExcelHelper excelHelper =XssfExcelHelper.getExcel(response);
            if(list.size()==0||list==null){
                list=new ArrayList<MerchantTransactionInfo>();
                MerchantTransactionInfo temp=new MerchantTransactionInfo();
                temp.setTransactionId("null");temp.setMerchantName("null");temp.setPaymentTypeName("null");
                temp.setOrigOrderNum("null");temp.setChcdName("null");temp.setTxamt("null");temp.setTerminalid("null");temp.setRespcdName("null");temp.setCreateTime(0L);
            }
            excelHelper.writeExcel(MerchantTransactionInfo.class,list,fieldNames,titles);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
